package cn.jinjimi.app.main.activity;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import cn.jinjimi.app.managers.ConfigManager;
import cn.jinjimi.app.modules.notify.NotificationDispatcher;
import cn.jinjimi.app.modules.notify.notifications.PushDTO;
import cn.jinjimi.framework.router.RouterHelper;

import java.util.Iterator;
import java.util.List;


/**
 *
 * @author zhenhua
 * @date 16/11/3
 */
public class RouteActivity extends Activity {

    public static final int GO_LOGIN = 0;
    private Intent[] routes;

    private ActivityManager activityManager = null;

    @Override
    public void onCreate(Bundle savedInstanceStates) {
        super.onCreate(savedInstanceStates);
        activityManager = (ActivityManager) super.getSystemService(Context.ACTIVITY_SERVICE);


        Uri uri = getIntent().getData();
        //网页端进来的
        if (uri != null) {
            routes = RouterHelper.getIntent(this, uri);
        } else { // 推送进来
            PushDTO notification = NotificationDispatcher.parseNotification(getIntent());
            if (notification != null) {
                routes = RouterHelper.getIntent(this, notification);
            }
        }

        Intent next;
        if (routes == null) {
            if (!isAppStarted()) {
                startActivity(new Intent(this, StartActivity.class));
            }
            finish();
        }

        if (routes != null && routes.length > 0) {
            next = routes[0];

            if (routes.length > 1) {
                startActivityForResult(next, GO_LOGIN);
            } else {
                if (isAppStarted()) {
                    startActivity(next);
                } else {
                    Intent[] intents = new Intent[2];
                    intents[0] = new Intent(this, MainActivity.class);
                    intents[1] = routes[0];
                    startActivities(intents);
                }
                finish();
            }
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (requestCode == GO_LOGIN && resultCode == RESULT_OK) {
            if (routes != null && routes.length > 1) {
                if (isAppStarted()) {
                    startActivity(routes[1]);
                } else {
                    Intent[] intents = new Intent[2];
                    intents[0] = new Intent(this, MainActivity.class);
                    intents[1] = routes[1];
                    startActivities(intents);
                }
            }
        } else {
            if (!isAppStarted()) {
                startActivity(new Intent(this, MainActivity.class));
            }
        }

        finish();
    }

    public boolean isAppStarted() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            return ConfigManager.getInstance().hasMainActivityStarted();
        } else {
            List<ActivityManager.RunningTaskInfo> taskInfos = activityManager.getRunningTasks(30);
            Iterator<ActivityManager.RunningTaskInfo> itInfo = taskInfos.iterator();
            while (itInfo.hasNext()) {
                ActivityManager.RunningTaskInfo info = itInfo.next();
                if (info.baseActivity.getClassName().equals("cn.jinjimi.sixapp.ui.activity.MainActivity"))
                    return true;
            }
        }
        return false;
    }
}
